package com.td.module.agro.controller.app.review;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.agro.controller.admin.review.vo.*;
import com.td.module.agro.convert.review.ReviewConvert;
import com.td.module.agro.dal.dataobject.review.ReviewDO;
import com.td.module.agro.service.review.ReviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "用户APP - 溯源信息")
@RestController
@RequestMapping("/agro/review")
@Validated
public class AppReviewController {

    @Resource
    private ReviewService reviewService;

    @PostMapping("/create")
    @ApiOperation("创建溯源信息")
    public CommonResult<Long> createReview(@Valid @RequestBody ReviewCreateReqVO createReqVO) {
        return success(reviewService.createReview(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新溯源信息")
    public CommonResult<Boolean> updateReview(@Valid @RequestBody ReviewUpdateReqVO updateReqVO) {
        reviewService.updateReview(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除溯源信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('agro:review:delete')")
    public CommonResult<Boolean> deleteReview(@RequestParam("id") Long id) {
        reviewService.deleteReview(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得溯源信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<ReviewUpdateReqVO> getReview(@RequestParam("id") Long id) {
        ReviewUpdateReqVO review = reviewService.getReview(id);
        return success(review);
    }

    @GetMapping("/list")
    @ApiOperation("获得溯源信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<ReviewRespVO>> getReviewList(@RequestParam("ids") Collection<Long> ids) {
        List<ReviewDO> list = reviewService.getReviewList(ids);
        return success(ReviewConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得溯源信息分页")
    public CommonResult<PageResult<ReviewRespVO>> getReviewPage(@Valid ReviewPageReqVO pageVO) {
        PageResult<ReviewDO> pageResult = reviewService.getReviewPage(pageVO);
        return success(ReviewConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出溯源信息 Excel")
    @OperateLog(type = EXPORT)
    public void exportReviewExcel(@Valid ReviewExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ReviewDO> list = reviewService.getReviewList(exportReqVO);
        // 导出 Excel
        List<ReviewExcelVO> datas = ReviewConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "溯源信息.xls", "数据", ReviewExcelVO.class, datas);
    }

}
